/**
 * User: alesj
 * Date: 2005.6.15
 * Time: 12:16:57
 * 
 * (C) Genera Lynx d.o.o.
 */

package com.generalynx.ecos.web.controller;

import com.generalynx.ecos.web.interceptor.ObjectAttacherInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.hibernate.SessionFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ObjectAttachMappingExceptionResolver extends SimpleMappingExceptionResolver {

    private ObjectAttacherInterceptor interceptor;

    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {
        ModelAndView mv = super.resolveException(request, response, handler, ex);
        if (mv != null) {
            try {
                interceptor.postHandle(request, response, null, mv);
            } catch (Exception e) {
                logger.warn("Handler interceptor threw exception: " + e.getMessage());
            }
        }
        return mv;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.interceptor = new ObjectAttacherInterceptor(false);
        this.interceptor.setSessionFactory(sessionFactory);
    }
}
